package comg.springboot.log.demo.util;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/4/15 14:52
 * @Version: 1.0
 */
public class Utils {

//    public static String abbr(String str, int length) {
//        return str.substring();
//    }

    /**
     * 截取字符串长度
     * @param value
     * @param maxSize
     * @return
     */
    public static String abbr(String value, int maxSize) {
        if(StringUtils.hasLength(value)){
            int valueLength = 0;
            String chinese = "[\u4e00-\u9fa5]";
            for (int i = 0; i < value.length(); i++) {
                String temp = value.substring(i, i + 1);
                if (temp.matches(chinese)) {
                    valueLength += 2;
                } else {
                    valueLength += 1;
                }
                if(valueLength == maxSize){
                    value = value.substring(0, i + 1);
                    break;
                }
                if(valueLength > maxSize){
                    if(temp.matches(chinese)){
                        value = value.substring(0, i);
                        break;
                    }else{
                        value = value.substring(0, i + 1);
                        break;
                    }
                }
            }

        }
        return value;
    }

    //时间戳转日期格式
    public static String timeStamp2Date(long seconds,String format) {
        if(seconds == 0 || seconds < 0){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(seconds));
    }

    public static void main(String[] args) {
        //System.out.println(abbr("abcd", 2));

        System.out.println(StringUtils.endsWithIgnoreCase("adV", "v"));
    }
}